import re

from captcha.fields import CaptchaField
from django import forms

from apps.users.utils.checkFieldByReg import checkPassword, checkAccount, checkEmail, checkMobilePhone, checkDateString
from apps.users.models import UserProfile

"""forms.Form"""
class RegisterForm(forms.Form):
    """
    用户注册表单
    """
    email = forms.EmailField(required=True)
    password = forms.CharField(required=True)
    captcha = CaptchaField(required=True, error_messages={"invalid": u"验证码输入错误!"})

    def clean_password(self):
        password = self.cleaned_data["password"]
        if checkPassword(password, None):
            return password
        raise forms.ValidationError(u"密码输入有误!", code="invalid")


class LoginForm(forms.Form):
    """
    用户登录表单
    """
    username = forms.CharField(required=True)
    password = forms.CharField(required=True)

    def clean_username(self):
        username = self.cleaned_data["username"]
        if checkAccount(username,None) or checkEmail(username) or checkMobilePhone(username):
            return username
        raise forms.ValidationError(u"用户名输入有误!", code="invalid")

    def clean_password(self):
        password = self.cleaned_data["password"]
        if checkPassword(password,None):
            return password
        raise forms.ValidationError(u"密码输入有误!", code="invalid")


class PasswordForgetForm(forms.Form):
    """
    用户找回密码表单
    """
    email = forms.EmailField(required=True, error_messages={"invalid": u"邮箱输入错误"})
    captcha = CaptchaField(required=True, error_messages={"invalid": u"验证码错误"})


"""forms.ModelForm"""
class PasswordResetForm(forms.ModelForm):
    """
    用户修改密码表单
    """
    class Meta:
        model = UserProfile
        fields = ["password","nickName"]

    def clean_password(self):
        password = self.cleaned_data["password"]
        pForPassword = re.compile("^[a-z][a-z0-9_-]{7,14}$")
        if pForPassword.match(password):
            return password
        raise forms.ValidationError(u"密码输入有误!",code="password1_invalid")

    def clean_nickName(self):
        password = self.cleaned_data["nickName"]
        pForPassword = re.compile("^[a-z][a-z0-9_-]{7,14}$")
        if pForPassword.match(password):
            return password
        raise forms.ValidationError(u"密码输入有误!", code="password2_invalid")


class ModifyImageForm(forms.ModelForm):
    """
    用户个人中心-修改头像
    """
    class Meta:
        model = UserProfile
        fields = ["image"]


class ModifyInfoForm(forms.ModelForm):
    """
    用户个人中心-修改用户信息
    """
    class Meta:
        model = UserProfile
        fields = ["nickName", "birthday", "sex", "address", "mobilePhone"]

    def clean_nickName(self):
        nickName = self.cleaned_data["nickName"]
        if len(nickName) > 0:
            return nickName
        raise forms.ValidationError(u"昵称输入有误!", code="nickName_invalid")

    def clean_birthday(self):
        birthday = self.cleaned_data["birthday"] # type = datetime
        if checkDateString(str(birthday)):
            return birthday
        raise forms.ValidationError(u"日期输入有误!", code="birthday_invalid")

    def clean_address(self):
        address = self.cleaned_data["address"]
        if len(address) > 0:
            return address
        raise forms.ValidationError(u"地址输入有误!", code="address_invalid")

    def clean_mobilePhone(self):
        mobilePhone = self.cleaned_data["mobilePhone"]
        if checkMobilePhone(mobilePhone):
            return mobilePhone
        raise forms.ValidationError(u"手机号码输入有误!", code="mobilePhone_invalid")